<template>
  <dialog-edit :visible.sync="dialogVisible" :title="$attrs.title" ref="userManageEdit" width="500"
               :saveLoading.sync="saveLoading"
               height="150" @save="save">
    <template slot="body">
      <div style="padding: 10px 0">
        <select-project-team v-model="userid" :clearable="true" :url="userApi" getPage="getPage" :queryValue="{}"
                             responseData="Users" @selectChanged="selectChange" searchKeyword="keywords"
                             column="Realname" clickValue="Userid" showValue="Realname"></select-project-team>
      </div>
<!--      <v-selectpage data="URL" :params="params" search-field="keywords" key-field="Profileid" show-field="Realname"-->
<!--                    :result-format="resultFormat" placeholder="请选择人员" title="人员"-->
<!--                    @values="selectChanged" style="margin-bottom: 20px"-->
<!--                    ref="selectPage"></v-selectpage>-->
    </template>
  </dialog-edit>
</template>

<script>
  import dialogEdit from '@/components/element/dialogEdit'
  import SelectProjectTeam from "@/components/element/select/SelectProjectTeam"
  // import {groupUserApi} from '@/api/organizationApiNow.js'
  import groupUserApi from '@/api/organizationApi/groupUserApi.js'
  import userApi from '@/api/userApi'

  export default {
    components: {
      dialogEdit,
      SelectProjectTeam
    },
    computed: {
      dialogVisible: {
        get: function () {
          return this.visible
        },
        set: function (value) {
          this.$emit('update:visible', value)
        }
      }
    },
    props: {
      visible: {
        type: Boolean,
        default() {
          return false;
        }
      },
      groupId: {
        type: [String, Number],
        default() {
          return null;
        }
      }
    },
    data() {
      return {
        saveLoading: false,
        userApi: userApi,
        userid: '',
        username: '',
        params: {
          pageSizeName: 'pageoffset',
          pageNumberName: 'p',
          url: 'v1/user/page',
          method: 'get'
        }
      }
    },
    methods: {
      selectChange (value) {
        if (value.length > 0) {
          this.username = value[0].Username
        }
      },
      resultFormat(res) {
        if (res && res.Users.length !== 0) {
          let list = res.Users
          return {
            totalRow: res.Total,
            list: list
          }
        }
      },
      save() {
        let obj = {
          GroupId: this.groupId,
          Userid: this.userid+'',
          Username: this.username
        }
        this.saveLoading = true
        groupUserApi.create(obj).then(res => {
          this.$XModal.message({message: res.message, status: res.code === 1 ? 'success' : 'error'})
          this.$emit('search')
          if (res.code === 1) this.dialogVisible = false
        }).finally(() => {
          this.saveLoading = false
        })
      },
      selectChanged(arr) {
        if (arr.length !== 0) {
          this.userid = arr[0].Profileid
          this.username = arr[0].Realname
        }
      }
    },
    watch: {
      visible: {
        handler(val) {
          if (val) {
            this.userid = ''
            this.username = ''
            this.$nextTick(() => {
              // this.$refs.selectPage.remove()
            })
          }
        }
      }
    }
  }
</script>

<style scoped>

</style>
